'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');
const HelperUtil = require('../utils/HelperUtil');
const StringUtil = require('../utils/StringUtil');
const Constant = require('../config/Constant');
const request = require('request');

class CommonController extends Controller {

    async captchaImage() {
        let captcha = await HelperUtil.captchaImage();
        let uuid = StringUtil.getUUID();
        let verifyKey = Constant.CAPTCHA_CODE_KEY + uuid;
        await this.ctx.service.redis.set(verifyKey, captcha.text, Constant.CAPTCHA_EXPIRATION * 60);
        let resp = {
            //captchaOnOff: null,
            uuid: uuid,
            img: captcha.data
        };
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getCountryList() {
        let chName = this.ctx.query.chName;
        const res = await this.ctx.service.country.getCountryList({chName: chName, delFlag: 0, parentId: 0});
        await this.ctx.service.redis.set(Constant.COUNTRY_LIST_KEY, JSON.stringify(res));
        return this.ctx.body = ResponseUtil.success(res);
    }


    //curl http://localhost:8072/pet/admin/common/upload/file -F "files=@/Users/mobao/Desktop/111.webp" -H "token: 222" -v
    async uploadFile() {
        //8080 https://du0mi3a8o1nps.cloudfront.net/2022-10-21-85d0c2b7-d08a-46ad-bfeb-f8acb2c3d40d.png
        //8001 https://d11he24itru2mu.cloudfront.net/2022-10-21-c6616710510211ed8721614dee04a46f.png
        const res = await this.ctx.service.upload.uploadOSSFile('');
        return this.ctx.body = ResponseUtil.success(res);
    }

    async batchUploadFile(){
        const res = await this.ctx.service.upload.batchUploadOSSFile('');
        return this.ctx.body = ResponseUtil.success(res);
    }

    async uploadPrivateFile(){
        const res = await this.ctx.service.upload.uploadFile('');
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getSignatureUrl(){
        const res = await this.ctx.service.upload.getSignatureUrl('');
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getS3File() {
        let fileUrl = this.ctx.query.fileUrl;
        if(!fileUrl){
            return this.ctx.body = ResponseUtil.success(null);
        }
        let arr = fileUrl.split('.net/');
        let key = arr[arr.length-1];
        const res = await this.ctx.service.upload.getPreSignatureUrl(key);
        return this.ctx.body = ResponseUtil.success(res);
    }


    async _requestFile(url) {
        return new Promise(function (resolve, reject) {
            let options = {
                url: url,
                encoding: null
            };
            request(options, function (error, response, buffer) {
                if (error) {
                    reject(error);
                } else {
                    resolve(buffer);
                }
            });
        });
    }
    async fileToBuffer(){
        let {url} = this.ctx.query;
        //url = url || 'https://d11he24itru2mu.cloudfront.net/2022-09-19-253b095b-085f-40af-bc76-752938de62e3.pdf'
        if (!url) {
            return this.ctx.body = ResponseUtil.success(null);
        }
        url = decodeURIComponent(url);
        let buffer = await this._requestFile(url);
        this.ctx.set(buffer.header);
        return this.ctx.body = buffer//ResponseUtil.success(buffer);
    }

    async uploadPublicFile() {
        const res = await this.ctx.service.upload.uploadPublicFile('');
        return this.ctx.body = ResponseUtil.success(res);
    }


    async getCountryChildren() {
        let parentId = this.ctx.params.parentId;
        let cacheKey = Constant.COUNTRY_LIST_KEY + parentId;
        let cachedList = await this.ctx.service.redis.get(cacheKey);
        if (cachedList) {
            console.log('cache country list');
            return this.ctx.body = ResponseUtil.success(cachedList);
        }
        const res = await this.ctx.service.country.getCountryList({delFlag: 0, parentId: parentId});
        await this.ctx.service.redis.set(cacheKey, JSON.stringify(res));
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getSystemTime(){
        let res = new Date().getTime();
        return this.ctx.body = ResponseUtil.success(res);
    }


    async getCourierCompanyData(){
        let res = require('../config/kuaidi');
        return this.ctx.body = ResponseUtil.success(res);
    }

    async eee(){
      /*  let params = {
            com:'shunfeng',
            num:'SF1692064409136',
            phone:'15873216103'
        }*/
    /*    let params = {
            com:'shentong',
            num:'773303181519955',
            phone:'15873216103'
        }
        let res = await this.ctx.service.kuaidi100.query(params);
        return this.ctx.body = res;*/
        let params = {
            com:'shentong',
            num:'773303181519955',
            phone:'15873216103'
        }
        let res = await this.ctx.service.aggPay.queryYibaoOrder2();


        console.log('res==',res)
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getCardList(){
        const fs = require('fs');
        const path = require('path');
        //let d = fs.createReadStream('/Users/mobao/WebstormProjects/pet-api/app/config/bank/card_list_dist.txt')
        //return this.ctx.body = d

        const data = fs.readFileSync(`${path.resolve(__dirname,'..')}/config/bank/card_list_dist.txt`, 'utf8');
        return this.ctx.body = ResponseUtil.success(data);
    }

    async getCustomerLink(){
        let res = {
            url:'https://work.weixin.qq.com/kfid/kfc10b4d0c929a3103b',
            corpId:'wwb3aea6b7e5068ad5'
        };
        return this.ctx.body = ResponseUtil.success(res);
    }


    async getWXLink(){
        const res = await this.ctx.service.wechat.getWXLink();
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getwxacodeunlimit(){
        const res = await this.ctx.service.wechat.getwxacodeunlimit();
        //this.ctx.set('Content-Type', res.contentType);
        return this.ctx.body = ResponseUtil.success(res.buffer);
    }

}

module.exports = CommonController;
